package com.carsystems.controller;

import com.carsystems.mapper.TipMapper;
import com.carsystems.pojo.Tip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TipController {

    @Autowired
    TipMapper tipMapper;

    //查看全部
    @GetMapping("/querTipList")
    public List<Tip> querTipList() {
        List<Tip> tipList = tipMapper.querTipList();
        return tipList;
    }

    //按照ts_id查询
    @GetMapping("/querTipByPrimaryKey")
    public Tip querTipByPrimaryKey(Integer ts_id) {
        Tip tips = tipMapper.querTipByPrimaryKey(ts_id);
        System.out.println(tips);
        return tips;
    }

    //增加一个信息
    @ResponseBody
    @PostMapping("/addTip")
    public String addTip(@RequestBody Tip tip) {
        tipMapper.addTip(tip);
        if (tip != null) {
            return "success";
        } else {
            return "fail";
        }
    }

    //修改信息
    @ResponseBody
    @PostMapping("/updateTip")
    public String updateTip(@RequestBody Tip tip){
        int row = tipMapper.updateTip(tip);
        tipMapper.querTipByPrimaryKey(tip.getTs_id());
        if (row==1){
            return "success";
        }else {
            return "fail";
        }
    }

    //删除
    @ResponseBody
    @GetMapping("/deleteTip")
    public String deleteTip(Integer ts_id){
        int row = tipMapper.deleteTip(ts_id);
        if (row == 1){
            return "success";
        }else {
            return "fail";
        }
    }
}
